import { IsString, IsOptional, IsNumber, Min, Max, IsEnum } from 'class-validator';
import { Transform } from 'class-transformer';
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';

export class UploadFileDto {
  @ApiProperty({ 
    type: 'string', 
    format: 'binary',
    description: 'File to upload' 
  })
  file: any;

  @ApiProperty({ 
    enum: ['batch'], 
    description: 'Purpose of the file upload',
    example: 'batch'
  })
  @IsEnum(['batch'])
  purpose: 'batch';
}

export class FileResponseDto {
  @ApiProperty({ description: 'File identifier', example: 'file_013Zva2CMHLNnXjNJJKqJ2EF' })
  id: string;

  @ApiProperty({ enum: ['file'], description: 'Object type' })
  type: 'file';

  @ApiProperty({ description: 'File purpose', example: 'batch' })
  purpose: string;

  @ApiProperty({ description: 'Original filename', example: 'batch_requests.jsonl' })
  filename: string;

  @ApiProperty({ description: 'File size in bytes', example: 15420 })
  size_bytes: number;

  @ApiProperty({ description: 'File creation timestamp', example: '2025-01-31T09:15:00.000000Z' })
  created_at: string;
}

export class ListFilesQueryDto {
  @ApiPropertyOptional({ 
    description: 'Number of files to return', 
    minimum: 1, 
    maximum: 1000, 
    default: 20 
  })
  @IsOptional()
  @IsNumber()
  @Min(1)
  @Max(1000)
  @Transform(({ value }) => parseInt(value))
  limit?: number = 20;

  @ApiPropertyOptional({ 
    description: 'Cursor for pagination - return results before this ID' 
  })
  @IsOptional()
  @IsString()
  before_id?: string;

  @ApiPropertyOptional({ 
    description: 'Cursor for pagination - return results after this ID' 
  })
  @IsOptional()
  @IsString()
  after_id?: string;

  @ApiPropertyOptional({ 
    description: 'Filter files by purpose',
    example: 'batch'
  })
  @IsOptional()
  @IsString()
  purpose?: string;
}

export class FilesResponseDto {
  @ApiProperty({ type: [FileResponseDto], description: 'Array of file objects' })
  data: FileResponseDto[];

  @ApiProperty({ 
    description: 'ID of the first file in the list', 
    nullable: true 
  })
  first_id: string | null;

  @ApiProperty({ 
    description: 'ID of the last file in the list', 
    nullable: true 
  })
  last_id: string | null;

  @ApiProperty({ description: 'Whether there are more files available' })
  has_more: boolean;
}

export class GetFileParamsDto {
  @ApiProperty({ description: 'File identifier' })
  @IsString()
  file_id: string;
}

export class DeleteFileParamsDto {
  @ApiProperty({ description: 'File identifier' })
  @IsString()
  file_id: string;
}

export class DeleteFileResponseDto {
  @ApiProperty({ description: 'File identifier' })
  id: string;

  @ApiProperty({ enum: ['file'], description: 'Object type' })
  type: 'file';

  @ApiProperty({ description: 'Deletion confirmation', example: true })
  deleted: boolean;
}